package se.janlindblom.ordning.helper;

/*
 * $Id: Ordnare.java 126 2010-01-04 19:33:23Z lindblom.jan $
 *
 * Copyright (c) 2009, Jan Lindblom
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * * Neither the name of the project nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

import java.util.Observable;
import java.util.Observer;
import se.janlindblom.ordning.typ.Minute;

/**
 * Ordnare - coordinates stuff, catches {@link Minute}s from the {@link Puls}
 * and puts them in a {@link Hink}.
 *
 * @author Jan Lindblom (lindblom.jan@gmail.com)
 * @version $Rev: 126 $
 */
public class Ordnare implements Observer {
    private Puls puls;
    private Hink hink;

    /**
     * Creates an Ordnare. the parameter selfcontained determines whether this
     * Ordnare has it's own {@link Puls} and {@link Hink}.
     *
     * If selfcontained is set to true, the Ordnare will have it's own internal
     * {@link Hink} and {@link Puls}. If set to false, this Ordnare will have to
     * be delegated its {@link Puls} and {@link Hink} through the setPuls() and
     * setHink() methods.
     *
     * @param selfcontained whether or not this Ordnare is selfcontained.
     */
    public Ordnare(boolean selfcontained) {
        if (selfcontained == true) {
            setHink(new Hink());
            setPuls(new Puls());
        } else {
            this.hink = null;
            this.puls = null;
        }
    }

    /**
     * Creates an Ordnare with a given {@link Hink} and {@link Puls}. This will
     * yield the same result as calling {@code new Ordnare(false);} and using
     * the setHink() and setPuls() methods.
     *
     * @param hink the {@link Hink} for this Ordnare to use.
     * @param puls the {@link Puls} for this Ordnare to use.
     */
    public Ordnare(Hink hink, Puls puls) {
        this(false);
        setHink(hink);
        setPuls(puls);
    }

    /**
     * @return the puls
     */
    public Puls getPuls() {
        return puls;
    }

    /**
     * @param puls the puls to set
     */
    public void setPuls(Puls puls) {
        this.puls = puls;
        this.puls.addObserver(this);
    }

    /**
     * @return the hink
     */
    public Hink getHink() {
        return hink;
    }

    /**
     * @param hink the hink to set
     */
    public void setHink(Hink hink) {
        this.hink = hink;
        this.hink.addObserver(this);
    }

    @Override
    public void update(Observable o, Object o1) {
        if (o.getClass().equals(Hink.class)) {
            // The Hink updated.
            if (o1.getClass().equals(Minute.class)) {
                // A Minute was put in the Hink!
            }
        } else if (o.getClass().equals(Puls.class)) {
            // The Puls updated.
            if (o1.getClass().equals(Minute.class)) {
                // There's a minute in the update!
                Minute m = (Minute) o1;
                // Put it in the Hink.
                if (hink != null)
                    hink.push(m);
            }
        }
    }

}
